package com.cart.protocol;
import com.cart.utils.FormatUtils;

import static com.cart.protocol.ProtocolDefine.DEVICE_TYPE;

/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2020-01-19
       Time     :  13:42
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class CreateContentFactory extends CommandFactory {

    public final static int DEFAULT_PROTOCOL_LENGTH = 15;


    public static String createContent(String commandWord, String moduleName, String action) {
        commandLengthContent = buildLengthContent(DEFAULT_PROTOCOL_LENGTH);

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(commandLengthContent).append(agentNumber);
        strBuilder.append(macAddress).append(DEVICE_TYPE);
        strBuilder.append(commandWord);
        strBuilder.append(moduleName);
        strBuilder.append(action);
        strBuilder.append(ProtocolDefine.CommandContent.BLANK);
        return strBuilder.toString();
    }

    public static String createContent(String commandWord, String moduleName, String action, String contentDetail) {
        //字节内容 |多一个字节|
        int contentLength = contentDetail.length() / 2 - 1 + DEFAULT_PROTOCOL_LENGTH;
        commandLengthContent = buildLengthContent(contentLength);
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(commandLengthContent).append(agentNumber);
        strBuilder.append(macAddress).append(DEVICE_TYPE);
        strBuilder.append(commandWord);
        strBuilder.append(moduleName);
        strBuilder.append(action);
        strBuilder.append(contentDetail);
        return strBuilder.toString();
    }


    public static String buildLengthContent(int length) {
        return FormatUtils.intValueTo4HexString(length);
    }

}
